package com.example.backend.controller;

import com.example.backend.dto.UserDTO;
import com.example.backend.entity.User;
import com.example.backend.service.UserService;
import com.example.backend.util.ApiResponse;
import com.example.backend.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @PostMapping("/login")
    public ApiResponse<Object> login(@RequestBody UserDTO userDTO) {
        User user = userService.getUserByUsername(userDTO.getUsername());
        if (user == null || !passwordEncoder.matches(userDTO.getPassword(), user.getPassword())) {
            return ApiResponse.error(401, "用户名或密码错误");
        }
        String token = JwtUtil.generateToken(user.getUsername(), user.getRole());
        return ApiResponse.success(Map.of("token", token));
    }

    @PostMapping("/register")
    public ApiResponse<String> register(@RequestBody UserDTO userDTO) {
        if (!"CUSTOMER".equals(userDTO.getRole())) {
            return ApiResponse.error(400, "只允许注册CUSTOMER角色");
        }
        User existingUser = userService.getUserByUsername(userDTO.getUsername());
        if (existingUser != null) {
            return ApiResponse.error(409, "用户名已存在");
        }
        User newUser = new User();
        newUser.setUsername(userDTO.getUsername());
        newUser.setPassword(passwordEncoder.encode(userDTO.getPassword()));
        newUser.setRole("CUSTOMER");
        userService.addUser(newUser);
        return ApiResponse.success("注册成功");
    }

    @PostMapping("/create")
    public ApiResponse<String> createUser(@RequestBody UserDTO userDTO) {
        User existingUser = userService.getUserByUsername(userDTO.getUsername());
        if (existingUser != null) {
            return ApiResponse.error(409, "用户名已存在");
        }
        User newUser = new User();
        newUser.setUsername(userDTO.getUsername());
        newUser.setPassword(userDTO.getPassword());
        newUser.setRole(userDTO.getRole());
        userService.addUser(newUser);
        return ApiResponse.success("创建成功");
    }

    @GetMapping("/{id}")
    public ApiResponse<User> getUserById(@PathVariable("id") Long id) {
        User user = userService.getUserById(id);
        if (user != null) {
            return ApiResponse.success(user);
        } else {
            return ApiResponse.error(404, "用户不存在");
        }
    }

    @GetMapping("/username/{username}")
    public ApiResponse<User> getUserByUsername(@PathVariable("username") String username) {
        User user = userService.getUserByUsername(username);
        if (user != null) {
            return ApiResponse.success(user);
        } else {
            return ApiResponse.error(404, "用户不存在");
        }
    }

    @GetMapping
    public ApiResponse<List<User>> getAllUsers() {
        List<User> users = userService.getAllUsers();
        return ApiResponse.success(users);
    }

    @PutMapping
    public ApiResponse<String> updateUser(@RequestBody User user) {
        userService.updateUser(user);
        return ApiResponse.success("更新成功");
    }

    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteUser(@PathVariable("id") Long id) {
        userService.deleteUser(id);
        return ApiResponse.success("删除成功");
    }
} 